package engine;

import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;
import ui.UserInterface;

public class WeightsCalculator extends SwingWorker<Void, Integer> {

    private Corpus corpus;
    private ArrayList<Document> documents;
    private UserInterface guiToUpdate;

    public WeightsCalculator(Corpus corpus, ArrayList<Document> documents) {
        this.corpus = corpus;
        this.documents = documents;
    }

    @Override
    protected Void doInBackground() throws Exception {
        int compteur = 0;
        int progress;
        
        for (Document currentDocument : this.documents) {
            compteur++;
            currentDocument.fillWeigths(this.corpus);
            
            progress = (int) ((double) compteur * 100 / (double) this.documents.size());
            publish(progress);
        }
        
        return null;
    }

    @Override
    protected void process(List<Integer> values) {
        for (Integer val : values) {
            guiToUpdate.updateProgressBarReadWeights(val);
        }
    }

    @Override
    protected void done() {
        guiToUpdate.weightsAreFilled();
    }

    public void setGuiToUpdate(UserInterface guiToUpdate) {
        this.guiToUpdate = guiToUpdate;
    }
}
